package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ExpandableListView;

import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityOnlineLinkBinding;
import com.sgb.kjwl.model.entity.packers.OnLineEntity;
import com.sgb.kjwl.utils.TextViewUtils;
import com.sgb.kjwl.view.ui.adapter.packers.LinkPeopleAdapter;
import com.sgb.kjwl.viewmodel.packers.OnLineLinkModel;
import com.sgb.link.router.PathTeam;
import com.swgk.core.common.base.BaseActivity;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: 王彬臣
 * @date: 2021/9/7
 * @Description: 工队发布-在线联系人
 */
@Route(path = PathTeam.OnLineLinkActivity)
public class OnLineLinkActivity extends BaseActivity<OnLineLinkModel, ActivityOnlineLinkBinding> {
    LinkPeopleAdapter mAdapter;
    List<OnLineEntity.ChildBean> departmentEntityList = new ArrayList<>();
    boolean isExpanded;
    private int mLastPosition;
    private int mGroupLastPosition;
    private String mImId;
    private String mImName;
    private String mDepartmentId;

    @Override
    protected int layoutId() {
        return R.layout.activity_online_link;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(OnLineLinkModel.class);
    }

    @Override
    protected void init() {
        mBinding.setOnClick(this);
        mBinding.actionBar.setOnBackClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mAdapter = new LinkPeopleAdapter(this, departmentEntityList);
        mBinding.expandList.setAdapter(mAdapter);
        mDepartmentId = getIntent().getStringExtra("departmentId");
        mImId = getIntent().getStringExtra("imId");
        mImName = getIntent().getStringExtra("imName");
        mViewModel.getList(mDepartmentId,mImId);
        mViewModel.ldMyDepartment.observe(this, data -> {
            mBinding.tvCompany.setText(data.getCompanyName());
            departmentEntityList.addAll(data.getChild());
            mAdapter.notifyDataSetChanged();
        });
        mBinding.expandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                OnLineEntity.ChildBean data = departmentEntityList.get(groupPosition);
                if (mGroupLastPosition != groupPosition || mLastPosition != childPosition) {
                    for (OnLineEntity.ChildBean childrenBean : departmentEntityList) {
                        for (OnLineEntity.ChildBean.OnlineConcatBean contactsBean : childrenBean.getOnlineConcat()) {
                            contactsBean.setSelect(false);
                        }
                    }
                }
                for (int i = 0; i < data.getOnlineConcat().size(); i++) {
                    if (childPosition == i ) {
                        data.getOnlineConcat().get(i).setSelect(!data.getOnlineConcat().get(i).isSelect());
                        if (data.getOnlineConcat().get(i).isSelect){
                            mImId = data.getOnlineConcat().get(i).getId();
                            mImName = data.getOnlineConcat().get(i).getName();
                            mDepartmentId = data.getDepartmentId();
                        }else {
                            mImId = "";
                            mImName = "";
                            mDepartmentId = "";
                        }
                    } else {
                        data.getOnlineConcat().get(i).setSelect(false);
                    }
                }

                mLastPosition = childPosition;
                mGroupLastPosition = groupPosition;
                mAdapter.notifyDataSetChanged();
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_company) {
            if (isExpanded) {
                isExpanded = false;
                mBinding.expandList.setVisibility(View.GONE);
                TextViewUtils.changeTextViewRightIcon(this, mBinding.tvCompany, R.mipmap.btn_expand1);
            } else {
                isExpanded = true;
                mBinding.expandList.setVisibility(View.VISIBLE);
                TextViewUtils.changeTextViewRightIcon(this, mBinding.tvCompany, R.mipmap.btn_expand_up_gray);
            }
        } else if (id == R.id.submit) {
            if (!TextUtils.isEmpty(mImId)&&!TextUtils.isEmpty(mImName)&&!TextUtils.isEmpty(mDepartmentId)){
                Intent intent = new Intent();
                intent.putExtra("mImId",mImId);
                intent.putExtra("mImName",mImName);
                intent.putExtra("mDepartmentId",mDepartmentId);
                setResult(RESULT_OK,intent);
                finish();
            }
        }
    }
}
